const ll inf = 1e18;

void solve()
{
  int n;
  cin >> n;

  vi a(n + 1);
  vl b(n + 1);

  rep(i, 1, n) {
    cin >> a[i];
    b[i] = b[i - 1] + a[i];
  }

  ll ans = inf;
  vl f;

  ll t = b[n];
  for (ll i = 2; i * i <= t; i++) {
    int cnt = 0;
    while (t % i == 0) {
      cnt++;
      t /= i;
    }
    if (cnt) {
      f.pb(i);
    }
  }

  if (t > 1) {
    f.pb(t);
  }

  for (auto &v : f) {
    ll res = 0;
    rep(i, 1, n) {
      res += min(b[i] % v, v - (b[i] % v));
    }
    ans = min(ans, res);
  }

  if (ans == inf) {
    cout << -1;
  } else {
    cout << ans;
  }
}